package com.yjlove.myplay.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.yjlove.myplay.MainActivity;
import com.yjlove.myplay.R;
import com.yjlove.myplay.mvptest.ui.LoginActivity;
import com.yjlove.myplay.utils.SP;
import com.yjlove.myplay.common.StringSpKey;

/**
 * 作者 YJlvoe
 * 创建时间 2017/1/19.
 */
public class SplashActivity extends AppCompatActivity{

    private Handler handler = new Handler();
    private Runnable runnable;
    private ImageView logo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        logo = (ImageView) findViewById(R.id.logo);

        initAnimation();

        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {

                if ("true".equals(SP.get(StringSpKey.FIRST_ENTER, "true"))){
                    Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
                    startActivity(intent);
                    SP.save(StringSpKey.FIRST_ENTER, "false");
                    finish();
                } else {
                    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }, 3000);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {

        if(event.getAction()==MotionEvent.ACTION_UP)
        {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            if (runnable != null)
                handler.removeCallbacks(runnable);
        }

        return super.onTouchEvent(event);
    }

    private void initAnimation() {

        // 旋转动画
        RotateAnimation rotateAnimation = new RotateAnimation(0 , 360
                , Animation.RELATIVE_TO_PARENT, 0.5f
                , Animation.RELATIVE_TO_SELF, 1f);
        //rotateAnimation.setInterpolator(new CycleInterpolator(100));
        rotateAnimation.setDuration(2000);

        // 透明动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation.setDuration(2000);

        // 缩放动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f
                , Animation.RELATIVE_TO_SELF, 0.5f
                , Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(2000);

        // 平移动画
        //TranslateAnimation translateAnimation = new TranslateAnimation(float fromXDelta, float toXDelta,float fromYDelta, float toYDelta)

        AnimationSet animationSet = new AnimationSet(false);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setFillAfter(true);
        animationSet.setInterpolator(new CycleInterpolator(2));
        logo.startAnimation(animationSet);
    }
}
